"use strict";
var _stat;

function init() {
	require(['echarts', 'vw.stat', 'select2-cn'], function(echarts, vw_stat) {
		var $tab1 = $('#tab-1');
		var $tab2 = $('#tab-2');
		var $tab3 = $('#tab-3');
		var $tab4 = $('#tab-4');

		var $mapTitle = $("#vw-map-title");
		var $chartTitle = $("#vw-chart-title");
		var $tableTitle = $("#vw-table-title");

		initSelect();
		initTab();

		function initSelect() {
			var $levelSelect = $('#vw-levelSelect');
			var $periodSelect = $('#vw-periodSelect');

			$levelSelect.select2({
				placeholder: '请选择汇总级别...',
				ajax: {
					url: 'temp/tj-objectLevel.json',
					type: 'GET',
					dataType: 'json',
					processResults: function(e) {
						return {
							results: e
						}
					}
				}
			});

			$periodSelect.select2({
				placeholder: '请选择调查期...',
				ajax: {
					url: 'temp/tj-period.json',
					type: 'GET',
					dataType: 'json',
					processResults: function(e) {
						return {
							results: e
						}
					}
				}
			});

			$levelSelect.on('select2:select', function(e) {
				if(e) {
					var results = e.params.data.text;
				}
			});

			$periodSelect.on('select2:select', function(e) {
				if(e.params) {
					var results = e.params.data.text;
				}
			});
		}

		function initTab() {
			var $chart = echarts.init($("#vw-chart")[0]);
			var $table = $('#vw-table');
			var $themeSelect = $('#vw-select-theme');
			var $fnChartDownload = $("#vw-fn-chart-download");
			var $fnTableDownload = $("#vw-fn-table-download");

			_stat = vw_stat();
			_stat.init("vw-map", $chart, $table, $themeSelect, $fnChartDownload, $fnTableDownload);
		}

		$mapTitle.text('农作物类型');
		$chartTitle.text('播种面积');
		$tableTitle.text('基点汇总表');
		loadData("temp/Self_Basic.json", 1, "pie");

		$tab1.on('shown.bs.tab', function() {
			$mapTitle.text('农作物类型');
			$chartTitle.text('播种面积');
			$tableTitle.text('基点汇总表');
			loadData("temp/Self_Basic.json", 1, "pie");
		});
		$tab2.on('shown.bs.tab', function() {
			$mapTitle.text('各期产量对比');
			$chartTitle.text('全年播种面积对比');
			$tableTitle.text('基点汇总表');
			loadData("temp/Comparison_Link.json", 2, "bar");
		});
		$tab3.on('shown.bs.tab', function() {
			$mapTitle.text('逐年对比');
			$chartTitle.text('上年同比');
			$tableTitle.text('基点汇总表');
			loadData("temp/Area_Comparison_Historical.json", 3, "bar");
		});
		$tab4.on('shown.bs.tab', function() {
			$mapTitle.text('样方内外轨迹点数量');
			$chartTitle.text('调查周期统计');
			$tableTitle.text('基点汇总表');
			loadData("temp/Trace_Date.json", 4, "pie");
		});

		function loadData(url, charttype, maptype) {
			$.ajax({
				url: url,
				type: 'GET',
				dataType: 'json',
				success: function(e) {
					if(e.success) {
						_stat.renderer("测试", e.data, charttype, maptype);
					}
				}
			})
		}
	});
}